package com.zyj;

import com.zyj.config.SpringConfig;
import com.zyj.config.SpringMvcConfig;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

import javax.servlet.Filter;

public class WebXmlInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {

    /**
     * 加载 spring 配置
     */
    @Override
    protected Class<?>[] getRootConfigClasses() {
        return new Class[] {SpringConfig.class};
    }

    /**
     * 加载 springmvc 配置
     */
    @Override
    protected Class<?>[] getServletConfigClasses() {
        return new Class[] {SpringMvcConfig.class};
    }

    /**
     * 获取前端控制器的映射信息
     */
    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }

    /**
     * 解决中文乱码问题
     * 
     * @return {@link Filter} 过滤器
     */
    @Override
    protected Filter[] getServletFilters() {
        Filter[] filters = new Filter[2];
        filters[0] = new CharacterEncodingFilter("UTF-8"); // 设置字符编码过滤器
        filters[1] = new ResourceUrlEncodingFilter(); // 静态资源url重写过滤器
        return filters;
    }
}